package com.zxy.h2demo;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.util.Log4jConfigurer;

import java.io.FileNotFoundException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * C3P0Connector.
 * 
 * Created by zxybird on 17/2/3.
 */
public class C3P0Connector {

  static {
    try {
      Log4jConfigurer.initLogging("classpath:log4j.properties");
    } catch (FileNotFoundException ex) {
      System.err.println("Cannot Initialize log4j");
    }
  }

  public static void main(String[] args) throws SQLException {
    ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:application-context.xml");
    ComboPooledDataSource dbSource = (ComboPooledDataSource) ctx.getBean("dbDataSource");
    Connection conn = dbSource.getConnection();
    Statement stmt = conn.createStatement();

    ResultSet rs = stmt.executeQuery("SELECT * FROM `trump`.`TEST_MEM`");

    while (rs.next()) {
      System.out.println(rs.getInt("ID") + "," + rs.getString("NAME"));
    }
    conn.close();
  }

}
